<?php

namespace App\Store\Models\Basic;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;

class OrderRemark extends Model
{
    protected $table = 'site_basic_order_remark';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表 
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['storeinfo'])->where(['cp_id' => $cp_id, 'is_del' => 1])->orderBy('create_at', 'desc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);


        $isgetall = intval($request->isgetall) ?? 0;

        $remark_title = $request->remark_title;
        if ($remark_title != '') {
            $query->where('remark_title', 'like', '%' . $remark_title . '%');
        }
        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        $store_id = $request->store_id;//下拉搜索
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }
        

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo(){
        return $this->hasOne(SiteStore::class, 'id', 'store_id');
    }
}
